/*
 * @Author: taoyuqing
 * @Date: 2022-12-31 15:50:31
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:spree/utils/utils.dart';

class SearchOptions extends StatefulWidget {
  final Function? onSearch;
  SearchOptions({Key? key, this.onSearch}) : super(key: key);

  @override
  State<SearchOptions> createState() => _SearchOptionsState();
}

class _SearchOptionsState extends State<SearchOptions> {
  List _sellectCollection = [
    {'label': 'Baccarat', 'isCheck': false},
    {'label': 'Poker', 'isCheck': false},
    {'label': 'Roulette', 'isCheck': false}
  ]; //局部刷新
  List _gamesType = [
    {'label': 'Baccarat', 'isCheck': false},
    {'label': 'Poker', 'isCheck': false},
    {'label': 'Roulette', 'isCheck': false}
  ]; //局部刷新

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Utils().createMaterialColor(color: Color(0xFF372E6F)),
        padding: EdgeInsets.all(20),
        child: Column(
          children: [
            Expanded(
              child: ListView(
                children: [
                  Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(bottom: 15),
                    child: Text(
                      '按条件筛选',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                      color: Utils().createMaterialColor(),
                    ),
                    child: ExpansionTile(
                      initiallyExpanded: true,
                      collapsedIconColor: Colors.white,
                      iconColor: Colors.white,
                      title: Text(
                        'Sellect Collection',
                        style: TextStyle(color: Colors.white),
                      ),
                      children: _createExpansionPanel(_sellectCollection),
                    ),
                  ),
                  _drawerDivider(),
                  Container(
                     decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                      color: Utils().createMaterialColor(),
                    ),
                    child: ExpansionTile(
                      initiallyExpanded: true,
                      collapsedIconColor: Colors.white,
                      iconColor: Colors.white,
                      title: Text(
                        'Game Type',
                        style: TextStyle(color: Colors.white),
                      ),
                      children: _createExpansionPanel(_gamesType),
                    ),
                  )
                ],
              ),
            ),
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
                if (widget.onSearch != null) {
                  widget.onSearch!();
                }
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(left: 15, right: 15),
                decoration: BoxDecoration(
                  color: Utils().createMaterialColor(),
                  borderRadius: const BorderRadius.all(Radius.circular(8.0)),
                ),
                height: 44,
                width: Utils().getScreenWidth(context) / 2,
                child: Text(
                  '搜索',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ));
  }

  List<Widget> _createExpansionPanel(List valueNotifier) {
    List<Widget> tiles = valueNotifier.asMap().keys.map((i) {
      return GestureDetector(
        onTap: () {
          setState(() {
            for (var i = 0; i < valueNotifier.length; i++) {
              valueNotifier[i]['isCheck'] = false;
            }
            valueNotifier[i]['isCheck'] = !valueNotifier[i]['isCheck'];
          });
        },
        child: ListTile(
          trailing: valueNotifier[i]['isCheck']
              ? Icon(
                  Icons.check,
                  color: Color(0xffB0AFEC),
                )
              : null,
          title: Text(
            valueNotifier[i]['label'],
            style: TextStyle(color: Color(0xffB0AFEC)),
          ),
        ),
      );
    }).toList();

    return tiles;
  }

  Widget _drawerDivider() {
    return Container(
      margin: EdgeInsets.only(top: 15, bottom: 15),
      child: const Divider(
        height: 1,
        color: Color(0xFF8B8AD1),
      ),
    );
  }
}
